package com.example.lemis.service.impl;

import com.example.lemis.common.exception.NotFoundException;
import com.example.lemis.model.domain.Division;
import com.example.lemis.mapper.DivisionMapper;
import com.example.lemis.model.dto.DivisionUpdateDTO;
import com.example.lemis.model.vo.DivisionVO;
import com.example.lemis.service.IAdminService;
import com.example.lemis.service.IDivisionService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.AllArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 管理行政区划 服务实现类
 * </p>
 *
 * @author lxp
 * @since 2025-06-24
 */
@Service
@AllArgsConstructor
public class DivisionServiceImpl extends ServiceImpl<DivisionMapper, Division> implements IDivisionService {
    private final DivisionMapper divisionMapper;
    private final IAdminService adminService;
    @Override
    public DivisionVO updateDivision(Long id, DivisionUpdateDTO divisionUpdateDTO) {

        adminService.auth();

        Division division = divisionMapper.selectById(id);
        if (division == null) {
            throw new NotFoundException("未找到对应行政区划");
        }

        if (divisionUpdateDTO.getName() != null) {
            division.setName(divisionUpdateDTO.getName());
        }

        if (divisionUpdateDTO.getRemark() != null) {
            division.setRemark(divisionUpdateDTO.getRemark());
        }
        divisionMapper.updateById(division);
        // 将更新后的实体转换为VO对象
        DivisionVO divisionVO = new DivisionVO();
        BeanUtils.copyProperties(division, divisionVO);
        return divisionVO;
    }

}
